package com.shengyuan.manage_os.old.ying.erpm;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.im.ui.LocationActivity;
import com.shengyuan.manage_os.im.ui.LocationInfo;
import com.shengyuan.manage_os.im.ui.chatting.ChattingActivity;
import com.shengyuan.manage_os.old.ying.erpm.bll.IBaseActivity;
import com.shengyuan.manage_os.old.ying.erpm.controls.CurrentTime;
import com.shengyuan.manage_os.old.ying.erpm.controls.GpsButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.JieXieMingCheng;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.RecordButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu;
import com.shengyuan.manage_os.old.ying.erpm.controls.VideoButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu.OnPopClickListener;
import com.shengyuan.manage_os.old.ying.erpm.db.Actual_Data_Table;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;
import com.shengyuan.manage_os.utils.DeviceUtils;
import com.shengyuan.manage_os.utils.ViewTools;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import static com.shengyuan.manage_os.old.ying.erpm.SettingActivity.InitGoldelData;

public abstract class BaseActivity extends Activity implements IBaseActivity, OnClickListener, OnLongClickListener {
	public PictureButton Picture;
	public static final int RESULT_CAPTURE_IMAGE= 1, REQUEST_CODE_TAKE_VIDEO=2, RESULT_CAPTURE_RECORDER_SOUND=3, RESULT_CAPTURE_GPS=4;
	
	protected ArrayList<String> mArrayList; ArrayList<Integer> RowID;
	protected String[] mCar;
	
	Spinner spinner_project_o=null;
	Spinner spinner_jindu_o=null;
	protected Button btn_back, btn_save;
	protected Spinner spinner_project, spinner_jindu, spinner_project_baoxiao, spinner_jindu_baoxiao,spinner_car, spinner_fuwumingcheng;
	protected GpsButton GpsButton_Gps;
	protected VideoButton VideoButton_Video;
	protected RecordButton RecordButton_Record;
	protected PictureButton PictureButton_Car, PictureButton_CaiLiao, PictureButton_Chexing, PictureButton_ZhiLiang;
	protected PictureButton PictureButton_bengshang, PictureButton_bengma, PictureButton_bengdan;
	protected EditText  editText_car,editText_chexing, editText_memo, editText_DiDian, editText_SiJi;
	
	protected EditText editText_gongyingshang, editText_buyer, editText_jingzhong, editText_cailiao, editText_zhuangdi, editText_xiedi,editText_sunhao;
	protected EditText editText_driver;
	protected TextView title_actual_form_head, editText_to_baoyanglist;
	
	protected abstract void SaveJsonData();
	protected String MyID() {
		String Clazz = getClass().getName();
		String sub = Clazz.substring(Clazz.lastIndexOf("."),Clazz.length());
		String aaa = "com.ying.erpm"+sub;
		return aaa;
	}
	protected abstract void SaveSelfPic(String sPath);
	
	public static String[] A_PROJECTNAME,A_TASKNAME, A_GONGCHENGMINGCHENG, A_GONGCHENGMINGCHENG_XIANGMU;
	public static JSONArray J_GONGCHENGMINGCHENG,J_GONGCHENGMINGCHENG_XIANGMU; //必须填充,才有TASKID

	protected JSONObject mJson;
	protected static int mMaxid;
	protected Boolean IsJson1;
	protected String mPictureLittle;
	protected boolean hasToprinte= false;

	protected int mFinished = 0, mPrintenabled=1, mPrintcount ;
    public Handler pHandler=new Handler(){  
        @Override  
        public void handleMessage(Message msg) {  
            super.handleMessage(msg);  
            switch(msg.what)  
            {  
                case 21:  
                	//PlayVideo(VideoButton.VideoFileName);break;
                	break;
                case GpsButton.ID_PLAYER:
	    			Intent intent = new Intent( MainApplication.currentActivity, LocationActivity.class);
	    			MainApplication.currentActivity.startActivityForResult(intent,5000);
                break;  
            }  
        }  
    };  
    
    
	@Override
	protected void onResume() {
		super.onResume();
		CurrentTime.Stop = false;
		if (PictureButton_Car!=null) {
			PictureButton_Car.postInvalidate();
		}
		if (PictureButton_CaiLiao!=null) {
			PictureButton_CaiLiao.postInvalidate();
		}
		if (PictureButton_Chexing!=null) {
			PictureButton_Chexing.postInvalidate();
		}
		if (PictureButton_ZhiLiang!=null) {
			PictureButton_ZhiLiang.postInvalidate();
		}
		if (VideoButton_Video!=null) {
			VideoButton_Video.postInvalidate();
		}
		
	}
	@Override
	protected void onStop() {
		CurrentTime.Stop = true;
		super.onStop();
	}
	public  void StartCamera( String PictureLittle) {
		mPictureLittle = PictureLittle;
		Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//android.media.action.IMAGE_CAPTURE  android.provider.MediaStore.ACTION_IMAGE_CAPTURE
		Uri imageFileUri = Uri.fromFile(new File( PictureButton.PictureFileName));
		imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageFileUri);
		startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
		//startActivity(imageCaptureIntent);
	}
	public void videoMethod() {
		Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//		Uri uri = Uri.fromFile(out);
//		intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
		startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO);
	}
	public void soundRecorderMethod() {
		Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
		intent.setType("audio/amr");
		startActivityForResult(intent, RESULT_CAPTURE_RECORDER_SOUND);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainApplication.currentActivity = this;
		InitGoldelData();


	}


	protected void onCreate_copy(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		MainApplication.currentActivity = this;
	}
	protected void SpinnerToEditText(int index)
	{
		
	}
    @Override
	public void onClick(View view) {
    	view.setEnabled(false);
    	if (view.getId()== btn_back.getId()){
    		this.Back();
		}else if(view.getId()== btn_save.getId()) {
			if (MainApplication.APPDEBUG || IsPassed()) {
				SaveJsonData();
				Save(IsJson1);
				Save_Has_Finished();
				Back();
			}
		}
    	view.setEnabled(true);
	}

    protected void PopSiJi( EditText view ) {
    	PopMenuFormField(view, "SiJi");
	}
    
    protected void PopMenuFormField( EditText view, String field ) {
    	String data=getShiShiField(field); 
		if (!data.equals("")) {
			SourceSelectMenu menu = new SourceSelectMenu(view);
			menu.setArrays(data.split(","));
			menu.show();
			menu.setOnPopClickListener(new onPopClickListener());
		}
	}
    
    @Override
	public boolean onLongClick(View view) {
    	String data="";
		switch (view.getId()) {
		case R.id.editText_driver:
			Utility.HideEditFouce(editText_driver);
			PopSiJi( editText_driver);
			break;
		case R.id.editText_SiJi:
			Utility.HideEditFouce(editText_SiJi);
			PopSiJi( editText_SiJi);
			break;		
		case R.id.editText_didian:
				Utility.HideEditFouce(editText_DiDian);
		        //data = ECApplication.pShareUtil.ReadParam(Submit_System_Setting_Data_Task.T_DIDIAN);
			try {
				data = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("DiDian");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
				if (!data.equals("")) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_DiDian);
					menu.setArrays(data.split("\n"));
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			break;
		case R.id.editText_car:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_car);
				if (Submit_System_Setting_Data_Task.A_ZIYOUSHILUN!=null 
						&& Submit_System_Setting_Data_Task.A_ZIYOUSHILUN.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_car);
					menu.setArrays(Submit_System_Setting_Data_Task.A_ZIYOUSHILUN);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}
			break;
		case R.id.editText_chexing:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_chexing);
				if (Submit_System_Setting_Data_Task.A_CHEXING!=null 
						&& Submit_System_Setting_Data_Task.A_CHEXING.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_chexing);
					menu.setArrays(Submit_System_Setting_Data_Task.A_CHEXING);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}
			break;				
		case R.id.editText_gongyingshang:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_gongyingshang);
				if (Submit_System_Setting_Data_Task.A_GONYINGSHANG!=null 
						&& Submit_System_Setting_Data_Task.A_GONYINGSHANG.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_gongyingshang);
					menu.setArrays(Submit_System_Setting_Data_Task.A_GONYINGSHANG);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}
			break;
		case R.id.editText_cailiao:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_cailiao);
				if (Submit_System_Setting_Data_Task.A_CAILIAOMINGCHENG!=null 
						&& Submit_System_Setting_Data_Task.A_CAILIAOMINGCHENG.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_cailiao);
					menu.setArrays(Submit_System_Setting_Data_Task.A_CAILIAOMINGCHENG);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}
			break;
		case R.id.editText_zhuangdi:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_zhuangdi);
				if (Submit_System_Setting_Data_Task.A_ZHUANGDI!=null 
						&& Submit_System_Setting_Data_Task.A_ZHUANGDI.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_zhuangdi);
					menu.setArrays(Submit_System_Setting_Data_Task.A_ZHUANGDI);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}		
			break;
		case R.id.editText_xiedi:
			if (IsDoubleClick()) {
				Utility.HideEditFouce(editText_xiedi);
				if (Submit_System_Setting_Data_Task.A_XIEDI!=null 
						&& Submit_System_Setting_Data_Task.A_XIEDI.length>0) {
					SourceSelectMenu menu = new SourceSelectMenu(editText_xiedi);
					menu.setArrays(Submit_System_Setting_Data_Task.A_XIEDI);
					menu.show();
					menu.setOnPopClickListener(new onPopClickListener());
				}
			}	
			break;
		default:
			return false;
		}
		return true;
	}
    
    protected void Save_Has_Finished() {
		if (editText_chexing!=null) {
			MainApplication.pShareUtil.WriteParam( editText_chexing.getId()+"", editText_chexing.getText().toString());
		}		
		if (editText_gongyingshang!=null) {
			MainApplication.pShareUtil.WriteParam( editText_gongyingshang.getId()+"", editText_gongyingshang.getText().toString());		
		}
		if (editText_buyer!=null) {
			MainApplication.pShareUtil.WriteParam( editText_buyer.getId()+"", editText_buyer.getText().toString());		
		}
		if (editText_cailiao!=null) {
			MainApplication.pShareUtil.WriteParam( editText_cailiao.getId()+"", editText_cailiao.getText().toString());
		}
		if (editText_zhuangdi!=null) {
			MainApplication.pShareUtil.WriteParam( editText_zhuangdi.getId()+"", editText_zhuangdi.getText().toString());
		}
		if (editText_xiedi!=null) {
			MainApplication.pShareUtil.WriteParam( editText_xiedi.getId()+"", editText_xiedi.getText().toString());
		}	
	}
    
	public class onPopClickListener implements OnPopClickListener {
		@Override
		public void onPopClick(int index, String text, View anchor) {
			EditText etext = (EditText) anchor;
			if (etext!=null) {
				etext.setText(text);
			}
		}
	}

	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if ( requestCode == RESULT_CAPTURE_IMAGE) {
			try {
				if (RESULT_OK == resultCode) {
					Bitmap bitmap = Utility.getImageThumbnail(Picture.PictureFileName, 120, 120);
					File f = new File(mPictureLittle);
//					f.createNewFile();
					FileOutputStream fout = new FileOutputStream(f);
					bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fout);
					fout.flush();
					fout.close();
					
//					bitmap = Utility.getImageThumbnail(Picture.PictureFileName, 1024, 768);
//					f = new File(Picture.PictureFileName);
//					f.delete();
//					f.createNewFile();
//					fout = new FileOutputStream(f);
//					bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fout);
//					fout.flush();
//					fout.close();
					String string = "";
					
				}
				//Picture.postInvalidate();
			} catch (Exception e) {
				e.printStackTrace();
				Utility.MessageBox(this,e.getMessage());
			}
		}else if (requestCode == REQUEST_CODE_TAKE_VIDEO) {
			 String strVideoPath;
			 if (resultCode == RESULT_OK) {
				 Uri uriVideo = data.getData();
				 Cursor cursor=this.getContentResolver().query(uriVideo, null, null, null, null);
				 if (cursor.moveToNext()) {
				         strVideoPath = cursor.getString(cursor.getColumnIndex("_data"));
				         String s = cursor.getString(cursor.getColumnIndex("_display_name"));
				         //strVideoPath = cursor.getString(cursor.getColumnIndex("_display_name"));
				         try {
				        	 File det = new File(VideoButton.VideoFileName);
				        	 if (!det.exists()) {
								
				        		File out = new File(VideoButton.VideoFileName);
								try {
									if (!out.exists()) {
										out.createNewFile();				
									}
								} catch (IOException e) {
									e.printStackTrace();
								}
				        	 }
					         File file = new File(strVideoPath);
					         file.renameTo( det );
					         if (det.exists()) {
					        	 Utility.MessageBox(this,"录像保存成功!");
							}
						} catch (Exception e) {
							Utility.MessageBox(this,e.getMessage());
					}
				 }
			}
		}else if(requestCode == 5000 && resultCode == ChattingActivity.RESULT_OK){
			LocationInfo info = (LocationInfo) data.getSerializableExtra("location");
			GpsButton_Gps.mlocation = info.getAddress();
			GpsButton_Gps.x = info.getLat();
			GpsButton_Gps.y = info.getLon();
			GpsButton.title.setText(info.getAddress());
//			Toast.makeText(this,info.getAddress(),Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void Save(Boolean isJson1) {
		try {
			Actual_Data_Table dbo = new Actual_Data_Table(this);
			if (isJson1) {
				System.out.println("############======!!!!======++"+mJson.toString());
				System.out.println("############======!!!!======++"+LoginActivity.pmUser.ID);
				System.out.println("############======!!!!======++"+mFinished);
				System.out.println("############======!!!!======++"+mPrintenabled);
				String time = CurrentTime.CurrentTimeMillis.substring(2,10);
				mMaxid = dbo.Save_Data(MyID(), mJson.toString(), "", 
						LoginActivity.pmUser.ID+"", mFinished, 0, mPrintenabled, 
						time, mPrintcount);
			}else {
				System.out.println("############======!!!!JSON2======++"+mJson.toString());
				if (mMaxid==0) {

					throw new Exception("没有选择正确的车辆");
				}
				dbo.Update_Data(mMaxid, dbo.JSON2, mJson.toString());
				dbo.Update_Data(mMaxid, dbo.FINISHED, "1");
			}
			
			//dbo.Update_Data(maxid, dbo.JSON2, "value");
			//ArrayList<String> list = dbo.getArray_Finished();
			dbo.close();
			
			//编号目录
			String dest = MainApplication.filePath + mMaxid;
			Utility.CreateFolder(dest);
			
			String s;
			if (isJson1) {
				//附件 json1 目录
				s = dest + "/" + Actual_Data_Table.JSON1;
				Utility.CreateFolder(s);
			}else {
				//附件 json2 目录
				s = dest + "/" + Actual_Data_Table.JSON2;
				Utility.CreateFolder(s);
			}
			
			if (VideoButton_Video!=null) {
				Utility.CopyDest(VideoButton_Video.VideoFileName, s + "/" + VideoButton_Video.FILENAME);
			}
			if (RecordButton_Record!=null) {
				Utility.CopyDest(RecordButton_Record.VoiceFileName, s + "/" + RecordButton_Record.FILENAME);
			}
			
			SaveSelfPic(s);
			Utility.MessageBox(this,"表单保存成功!");

			System.out.println("@@@@@####Exception====表单保存成功");

//			Back();
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("@@@@@####Exception===="+e.toString());
			Utility.MessageBox(this,e.getMessage());
		}
	}

	@Override
	public Boolean IsPassed() {
		String value;
		if (spinner_project!=null) {
			value = spinner_project.getSelectedItem().toString();
			if (value.equals("")) {
				Utility.MessageBox( this,getResources().getString(R.string.spinner_projectname_no));
				return false;
			}
		}
		if (spinner_jindu!=null) {
			if (spinner_jindu.getSelectedItem()!=null) {
				value = spinner_jindu.getSelectedItem().toString();
				if (value.equals("")) {
					Utility.MessageBox(this, getResources().getString(R.string.spinner_taskname_no));
					return false;
				}
			}
		}
		
		if (editText_car!=null) {
			value = editText_car.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_car.getHint().toString());
				return false;
			}
			if (IsChinese(value.charAt(0))) {
				Utility.MessageBox(this,"车辆号码不符合真实性要求!");
				return false;
			}
		}		
		if (editText_chexing != null) {
			value = editText_chexing.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_chexing.getHint().toString());
				return false;
			}
		}		
		if (editText_cailiao!=null) {
			value = editText_cailiao.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_cailiao.getHint().toString());
				return false;
			}	
		}	
		if (editText_zhuangdi != null) {
			value = editText_zhuangdi.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_zhuangdi.getHint().toString());
				return false;
			}
		}	
		if (editText_xiedi != null) {
			value = editText_xiedi.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_xiedi.getHint().toString());
				return false;
			}
		}		
		if (editText_gongyingshang!=null) {
			value = editText_gongyingshang.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_gongyingshang.getHint().toString());
				return false;
			}
		}
		if (editText_buyer!=null) {
			value = editText_buyer.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,editText_buyer.getHint().toString());
				return false;
			}
		}		
		if (GpsButton_Gps!=null) {
			if (GpsButton_Gps.mlocation.equals("")) {
			Utility.MessageBox(this, GpsButton_Gps.hint );
			return false;
			}
		}
		return true;
	}
	
	private boolean IsChinese(char c) {
		//Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		return !String.valueOf(c).matches("[\u4e00-\u9fa5]");
	}
	
	@Override
	public void Back() {
		this.finish();
		//删除图片
		//File file = new File(MainApplication.cameraPath);
		File[] allFiles = new File(MainApplication.cameraPath).listFiles();
		for (int i = 0; i < allFiles.length; i++) {
			if (allFiles[i].isFile() && allFiles[i].getName().endsWith(".jpg")) {
				allFiles[i].delete();
			}
		}
		
		//打开打印
		if (mFinished == 1&& mPrintenabled == 1 && hasToprinte==false) {
			hasToprinte = true;
			Message message = WorkSpaceActivity.Instance.mHandler.obtainMessage();
			message.obj = mJson.toString();
			message.what = 1;
			WorkSpaceActivity.Instance.mHandler.sendMessage(message);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return super.onTouchEvent(event);
	}
	@Override
	public void InitControls() {
		init();
		String value = "";
		title_actual_form_head = (TextView) findViewById(R.id.title_actual_form_head);
		spinner_project = (Spinner) findViewById(R.id.spinner_project);spinner_project_baoxiao = (Spinner) findViewById(R.id.spinner_project_baoxiao);
		spinner_jindu = (Spinner) findViewById(R.id.spinner_task);spinner_jindu_baoxiao = (Spinner) findViewById(R.id.spinner_task_baoxiao);
		spinner_car = (Spinner) findViewById(R.id.spinner_car);
		spinner_fuwumingcheng = (Spinner) findViewById(R.id.spinner_fuwumingcheng);
		
		editText_driver = (EditText) findViewById(R.id.editText_driver);
		editText_to_baoyanglist =(TextView) findViewById(R.id.editText_to_baoyanglist);
		
		editText_car = (EditText) findViewById(R.id.editText_car);
		editText_chexing = (EditText) findViewById(R.id.editText_chexing);
		editText_gongyingshang = (EditText) findViewById(R.id.editText_gongyingshang);
		editText_buyer = (EditText) findViewById(R.id.editText_buyer);
		editText_jingzhong = (EditText) findViewById(R.id.editText_jingzhong);
		editText_cailiao = (EditText) findViewById(R.id.editText_cailiao);
		editText_zhuangdi = (EditText) findViewById(R.id.editText_zhuangdi);
		editText_xiedi = (EditText) findViewById(R.id.editText_xiedi);
		editText_sunhao = (EditText) findViewById(R.id.editText_sunhao);
		GpsButton_Gps = (GpsButton) findViewById(R.id.GpsButton_Gps);
		VideoButton_Video = (VideoButton) findViewById(R.id.VideoButton_Video);
		RecordButton_Record = (RecordButton) findViewById(R.id.RecordButton_Record);
		PictureButton_Car = (PictureButton) findViewById(R.id.PictureButton_Car);
		PictureButton_CaiLiao = (PictureButton) findViewById(R.id.PictureButton_CaiLiao);
		PictureButton_Chexing = (PictureButton) findViewById(R.id.PictureButton_Chexing);

		PictureButton_bengshang = (PictureButton) findViewById(R.id.PictureButton_bengshang);
		PictureButton_bengma = (PictureButton) findViewById(R.id.PictureButton_bengma);
		PictureButton_bengdan = (PictureButton) findViewById(R.id.PictureButton_bengdan);
//		PictureButton_ZhiLiang = (PictureButton) findViewById(R.id.PictureButton_ZhiLiang);
		editText_memo = (EditText) findViewById(R.id.editText_memo);
		editText_DiDian = (EditText) findViewById(R.id.editText_didian);
		editText_SiJi = (EditText) findViewById(R.id.editText_SiJi);
		
		try {
			if (editText_to_baoyanglist!=null) {
				editText_to_baoyanglist.setOnClickListener(this);
			}
			if (editText_driver!=null) {
				editText_driver.setOnLongClickListener(this);
				editText_driver.setInputType(InputType.TYPE_NULL);
			}
			if (editText_DiDian!=null) {
				editText_DiDian.setOnLongClickListener(this);
				value = MainApplication.pShareUtil.ReadParam(editText_DiDian.getId()+"");
				editText_DiDian.setText(value);			
				editText_DiDian.setInputType(InputType.TYPE_NULL);
			}
			if (editText_SiJi!=null) {
				editText_SiJi.setOnLongClickListener(this);
				editText_SiJi.setInputType(InputType.TYPE_NULL);
			}		
			if (editText_car!=null) {
//				editText_car.setOnClickListener(this);
				editText_car.setOnLongClickListener(this);
//				editText_car.setInputType(InputType.TYPE_NULL);
			}
			if (editText_chexing!=null) {
				//editText_chexing.setOnLongClickListener(this);
				//value = ECApplication.pShareUtil.ReadParam(editText_chexing.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("CheLiangXingHao");
				editText_chexing.setText(value);			
				editText_chexing.setInputType(InputType.TYPE_NULL);
			}		
			if (editText_gongyingshang!=null) {
				//editText_gongyingshang.setOnLongClickListener(this);
				//value = ECApplication.pShareUtil.ReadParam(editText_gongyingshang.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("GongYingShangJia");
				editText_gongyingshang.setText(value);	
				editText_gongyingshang.setInputType(InputType.TYPE_NULL);
			}
			if (editText_buyer!=null) {
				//editText_buyer.setOnLongClickListener(this);
				//value = ECApplication.pShareUtil.ReadParam(editText_buyer.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("GongYingShangJia");
				editText_buyer.setText(value);	
				editText_buyer.setInputType(InputType.TYPE_NULL);
				//editText_buyer.setInputType(InputType.TYPE_NULL);
			}
			if (editText_cailiao!=null) {
				//editText_cailiao.setOnLongClickListener(this);
				//value = ECApplication.pShareUtil.ReadParam(editText_cailiao.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("CaiLiaoMingCheng");
				editText_cailiao.setText(value);
				editText_cailiao.setInputType(InputType.TYPE_NULL);
			}
			if (editText_zhuangdi!=null) {
				//editText_zhuangdi.setOnLongClickListener(this);	
				//value = ECApplication.pShareUtil.ReadParam(editText_zhuangdi.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("ZhuangDi");
				editText_zhuangdi.setText(value);
				editText_zhuangdi.setInputType(InputType.TYPE_NULL);
			}
			if (editText_xiedi!=null) {
				//editText_xiedi.setOnLongClickListener(this);	
				//value = ECApplication.pShareUtil.ReadParam(editText_xiedi.getId()+"");
				value = Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(Submit_System_Setting_Data_Task.mID).getString("XieDi");
				editText_xiedi.setText(value);
				editText_xiedi.setInputType(InputType.TYPE_NULL);
			}		
			if (editText_memo!=null) {
				editText_memo.setOnLongClickListener(this);
			}
			if (editText_sunhao!=null) {
				editText_sunhao.setText("0");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

//		if (spinner_project!=null) {
//			spinner_project.setOnItemSelectedListener(new OnItemSelectedListener() {
//				@Override
//				public void onNothingSelected(AdapterView<?> arg0) {
//				}
//
//				@Override
//				public void onItemSelected(AdapterView<?> arg0, View arg1,
//						int arg2, long arg3) {
//					String value = A_TASKNAME[arg2];
//					try {
//						if (spinner_jindu==null) { 
//							return;
//						}
//						String[] spStrings = value.split("\\|");
//						ArrayAdapter<String> adapter = new ArrayAdapter<String>(BaseActivity.this,
//						R.layout.simple_spinner_item, 
//						spStrings);
//						adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
//						spinner_jindu.setAdapter(adapter);
//					} catch (Exception e) {
//					}
//				}
//			});
//			
//
//				
//			if (WorkSpaceActivity.Instance.ProjectName!=null && WorkSpaceActivity.Instance.ProjectName.size() >0) {
//				 Iterator it= WorkSpaceActivity.Instance.ProjectName.keySet().iterator();
//				 A_PROJECTNAME = new String[WorkSpaceActivity.Instance.ProjectName.size()];
//				 A_TASKNAME= new String[WorkSpaceActivity.Instance.ProjectName.size()];
//				 int i = 0 ;
//				  while (it.hasNext())
//				  {
//					  String key=it.next().toString();
//					  A_PROJECTNAME[i]= key;
//					  A_TASKNAME[i]= WorkSpaceActivity.Instance.ProjectName.get(key);
//					  i++;
//				  }
//				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//						R.layout.simple_spinner_item, 
//						A_PROJECTNAME);
//				adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
//				spinner_project.setAdapter(adapter);
//			}else {
//				Utility.MessageBox("你没有配合的工程项目，如有问题请和项目负责人联系！");
//				this.finish();
//				return;
//			}
//		}
		

		if (spinner_project!=null) {
			spinner_project_o = spinner_project;
			if (spinner_jindu!=null) {
				spinner_jindu_o = spinner_jindu;
			}
		}else if (spinner_project_baoxiao!=null) {
			spinner_project_o = spinner_project_baoxiao;
			if (spinner_jindu_baoxiao!=null) {
				spinner_jindu_o = spinner_jindu_baoxiao;
			}
		}
		
		if (spinner_project_o!=null) {
			spinner_project_o.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					String value = A_TASKNAME[arg2];
					try {
						if (spinner_jindu_o ==null) { 
							return;
						}
						String[] spStrings = value.split("\\|");
						ArrayAdapter<String> adapter = new ArrayAdapter<String>(BaseActivity.this,
						R.layout.simple_spinner_item, 
						spStrings);
						adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
						spinner_jindu_o.setAdapter(adapter);
					} catch (Exception e) {
					}
				}
			});
			if (spinner_project_baoxiao!=null) {
				System.out.println("WorkSpaceActivity.Instance.ProjectName_BaoXiao!!!!!!!!!!!!======="+WorkSpaceActivity.Instance.ProjectName_BaoXiao);
				if (WorkSpaceActivity.Instance.ProjectName_BaoXiao!=null && WorkSpaceActivity.Instance.ProjectName_BaoXiao.size() >0) {
					 Iterator it= WorkSpaceActivity.Instance.ProjectName_BaoXiao.keySet().iterator();
					 A_PROJECTNAME = new String[WorkSpaceActivity.Instance.ProjectName_BaoXiao.size()];
					 A_TASKNAME= new String[WorkSpaceActivity.Instance.ProjectName_BaoXiao.size()];
					 int i = 0 ;
					  while (it.hasNext())
					  {
						  String key=it.next().toString();
						  A_PROJECTNAME[i]= key;
						  A_TASKNAME[i]= WorkSpaceActivity.Instance.ProjectName_BaoXiao.get(key);
						  i++;
					  }
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
							R.layout.simple_spinner_item, 
							A_PROJECTNAME);
					adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
					spinner_project_o.setAdapter(adapter);
				}else {
					Utility.MessageBox(this,"你没有配合的工程项目，如有问题请和项目负责人联系！");
					this.finish();
					return;
				}
			}	
			if (spinner_project != null) {
				System.out.println("WorkSpaceActivity.Instance.ProjectName======="+WorkSpaceActivity.Instance.ProjectName);
				if (WorkSpaceActivity.Instance.ShiShiTimeE==null) {
					Utility.MessageBox(this,"配合项目超时，请重新登录，如有问题请和项目负责人联系！");
					this.finish();
					return;
				}
				Boolean t = WorkSpaceActivity.Instance.ShiShiTimeE.getTime() > CurrentTime.getTime().getTime();
				if (!t) {
					Utility.MessageBox(this,"配合项目超时，请重新登录，如有问题请和项目负责人联系！");
					this.finish();
					return;
				}else if (WorkSpaceActivity.Instance.ProjectName!=null && WorkSpaceActivity.Instance.ProjectName.size() >0 ) {
					 Iterator it= WorkSpaceActivity.Instance.ProjectName.keySet().iterator();
					 A_PROJECTNAME = new String[WorkSpaceActivity.Instance.ProjectName.size()];
					 A_TASKNAME= new String[WorkSpaceActivity.Instance.ProjectName.size()];
					 int i = 0 ;
					  while (it.hasNext())
					  {
						  String key=it.next().toString();
						  A_PROJECTNAME[i]= key;
						  A_TASKNAME[i]= WorkSpaceActivity.Instance.ProjectName.get(key);
						  i++;
					  }
					ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
							R.layout.simple_spinner_item, 
							A_PROJECTNAME);
					adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
					spinner_project_o.setAdapter(adapter);
				}else {
					Utility.MessageBox(this,"你没有配合的工程项目，如有问题请和项目负责人联系！");
					this.finish();
					return;
				}
			}

		}
//		if (spinner_jindu!=null) {
//			if (A_GONGCHENGMINGCHENG!=null) {
//				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
//						R.layout.simple_spinner_item, 
//						A_GONGCHENGMINGCHENG);
//				adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
//				spinner_jindu.setAdapter(adapter);
//			}else {
//				Utility.MessageBox("你没有配合的进度任务，如有问题请和项目负责人联系！");
//				this.finish();
//				return;
//			}
//		}
		if (spinner_fuwumingcheng!=null) {
			if (Submit_System_Setting_Data_Task.A_FUWUXIANMU!=null) {
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
						R.layout.simple_spinner_item, 
						Submit_System_Setting_Data_Task.A_FUWUXIANMU);
				adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
				spinner_fuwumingcheng.setAdapter(adapter);
			}else {
				Utility.MessageBox(this,"工程服务项目数据没有下载");
				this.finish();
				return;
			}
		}
	}
    
	private void init() {
		btn_back = (Button) findViewById(R.id.btn_back);
		btn_save = (Button) findViewById(R.id.btn_save);
		btn_back.setOnClickListener(this);
		btn_save.setOnClickListener(this);
		
		//第一次进入隐藏键盘
		Utility.InitHideKeyboard(this.getWindow());
	}

	
	private long exittime = 0 ;
    public boolean IsDoubleClick() {
    	return true;
//        if(System.currentTimeMillis()-exittime>200){
//        	exittime=System.currentTimeMillis();
//        	return false;
//        }
//        else{
//        	exittime = 0 ;
//        	return true;
//        }
    }

    
	protected void JsonPicture() throws JSONException {
		if (PictureButton_Car!=null) {
			mJson.put( PictureButton_Car.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_Car.mFileName);
		}
		if (PictureButton_Chexing!=null) {
			mJson.put( PictureButton_Chexing.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_Chexing.mFileName);
		}
		if (PictureButton_CaiLiao!=null) {
			mJson.put( PictureButton_CaiLiao.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_CaiLiao.mFileName);
		}
		if (PictureButton_ZhiLiang!=null) {
			mJson.put( PictureButton_ZhiLiang.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_ZhiLiang.mFileName);
		}
	}
	
	protected void CopyPicDest(PictureButton button, String sPath) {
		Utility.CopyDest(button.FileName, sPath + "/" + button.mFileName);
	}
	protected void JsonMyID() throws JSONException {
		mJson = new JSONObject();

		mJson.put( getString(R.string.actual_form), MyID());
		if (title_actual_form_head!=null) {
			mJson.put( getString(R.string.actual_form_title), title_actual_form_head.getText().toString());
		}

	}
	protected void JsonMyTask(String key , String value) throws JSONException {
		mJson = new JSONObject();
		mJson.put( key , value);
	}
	protected void getCar() throws JSONException {
		if (editText_car!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_car), editText_car.getText().toString());
		}
		if (editText_chexing!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_chexing), editText_chexing.getText().toString());

		}
	}
	
	private void FilterData( String[] dest, String context)
	{
		HashMap<String, String> hash = new HashMap<String, String>();
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < dest.length; i++) {
			list.add(dest[i]);
			if (list.contains(context)) {
				return;
			}
		}
		list.add(context);
		dest = (String[]) list.toArray();
	}
	
	protected void getJieXieMingCheng() throws JSONException {
		if (JieXieMingCheng.mMachinery!=null) {
			mJson.put( getString(R.string.title_actual_form_edittext_jixiemingcheng),  JieXieMingCheng.mMachinery.Name );
		}
	}	
	protected void getTask() throws JSONException {
		if (spinner_project!=null) {
			mJson.put( getString(R.string.title_actual_form_spinner_project), spinner_project.getSelectedItem().toString());
		}else if (spinner_project_baoxiao!=null) {
			mJson.put( getString(R.string.title_actual_form_spinner_project), spinner_project_baoxiao.getSelectedItem().toString());
		}
		if (spinner_jindu!=null) {
			mJson.put( getString(R.string.title_actual_form_spinner_task), spinner_jindu.getSelectedItem().toString());
		}else if (spinner_jindu_baoxiao!=null) {
			mJson.put( getString(R.string.title_actual_form_spinner_task), spinner_jindu_baoxiao.getSelectedItem().toString());
		}
	}
	protected void getFuWuMingCheng() throws JSONException {
		if (spinner_fuwumingcheng!=null) {
			mJson.put( getString(R.string.title_actual_form_edittext_fuwumingcheng), spinner_fuwumingcheng.getSelectedItem().toString());
		}
	}
	
	protected void getZhuangdi() throws JSONException {
		if (editText_zhuangdi!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_zhuangdi), editText_zhuangdi.getText().toString());
		}
	}	
	protected void getXiedi() throws JSONException {
		if (editText_xiedi!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_xiedi), editText_xiedi.getText().toString());
		}
	}	
	protected void getSunHao() throws JSONException {
		if (editText_sunhao!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_sunhao), editText_sunhao.getText().toString());
		}
	}	
	protected void getGPS() throws JSONException {
		mJson.put( getString(R.string.title_actual_form_GpsButton_Gps_xy), GpsButton_Gps.x + "," + GpsButton_Gps.y);
		mJson.put( getString(R.string.title_actual_form_GpsButton_Gps), GpsButton_Gps.mlocation);
		
	}	
	protected void getCaiLiao() throws JSONException {
		if (editText_gongyingshang!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_gongyingshang), editText_gongyingshang.getText().toString());
		}
		if (editText_buyer!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_buyer), editText_buyer.getText().toString());
		}
		if (editText_cailiao!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_cailiao), editText_cailiao.getText().toString());
		}
	}
	protected void getCurrentUser() throws JSONException {
		mJson.put( getString(R.string.title_actual_form_editText_personal),  LoginActivity.pmUser.UserName );
		mJson.put( getString(R.string.title_actual_form_currenttime),  CurrentTime.getTimeString() );
	}	
	protected void getMemo() throws JSONException {
		if (editText_memo!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_memo), editText_memo.getText().toString());
		}
		if (VideoButton_Video !=null && VideoButton_Video.IsExist()) {
			mJson.put( getString(R.string.title_actual_form_VideoButton_Video), VideoButton_Video.FILENAME);
		}
		if (RecordButton_Record!=null && RecordButton_Record.IsExist()) {
			mJson.put( getString(R.string.title_actual_form_RecordButton_Record), RecordButton_Record.FILENAME);
		}
	}	
	
	protected boolean CheckEditNumber(EditText edit) {
		if (edit!=null) {
			String value = edit.getText().toString();
			if (value.equals("")|| value.equals("0")) {
				Utility.MessageBox(this,edit.getHint().toString());
				return false;
			}else {
				Pattern p = Pattern.compile("[0-9.]*"); 
			    Matcher m = p.matcher(value); 
			    if(!m.matches() ){
					Utility.MessageBox(this,edit.getHint().toString());
					return false;
			    }
			}			
		}
		return true;
	}
	
	protected boolean CheckEditEmpty(EditText edit) {
		String value;
		if (edit!=null) {
			value = edit.getText().toString();
			if (value.equals("")) {
				Utility.MessageBox(this,edit.getHint().toString());
				return false;
			}
		}
		return true;
	}
	protected boolean CheckEditStringMin5(EditText edit, int len) {
		String value = edit.getText().toString();
		if (value.equals("")) {
			Utility.MessageBox(this,edit.getHint().toString());
			return false;
		}		
		if (value.length()<len) {
			Utility.MessageBox(this,edit.getHint()+ "长度不得少于"+len+"个字");
			return false;
		}
		return true;
	}	
	protected boolean CheckEditStringMin5(EditText edit) {
		return CheckEditStringMin5(edit, 5);
	}
	protected void GetSpinnerCar2(String TypeName) {
		Actual_Data_Table dbo = new Actual_Data_Table(this);
//		mArrayList = dbo.getArray_Finished(0, TypeName);
		RowID = dbo.RowID;
//		GetCarArrayList(mArrayList);
		if (Submit_System_Setting_Data_Task.A_CHEPAI[0] != null) {
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
					R.layout.simple_spinner_item,
					Submit_System_Setting_Data_Task.A_CHEPAI);
			adapter.setDropDownViewResource(R.layout.simple_spinner_item_select);
			spinner_car.setAdapter(adapter);
			spinner_car.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int index, long arg3) {
					try {
						SpinnerToEditText(index);
//						mMaxid = getRowId(index);
						mMaxid = RowID.get(index);
					} catch (Exception e) {
					}
				}
			});
		}else {
			Utility.MessageBox(this,"没有可用的车辆");
			this.finish();
		}
	}
	
	private void GetCarArrayList( ArrayList<String> array) {
		int k = array.size();
		if ( k > 0) {
			mCar = new String[k] ;
			for (int i = 0; i < k; i++) {
				String s = array.get(i);
				try {
					JSONObject json = new JSONObject(s);
					mCar[i] = json.getString(getString(R.string.title_actual_form_editText_car));
				} catch (Exception e) {
				}
			}
		}
	}
	
	private String getContentForCar(int index, String key) {
		try {
			String s = mArrayList.get(index);
			JSONObject json = new JSONObject(s);
			return json.getString(key);
		} catch (Exception e) {
		}
		return "";
	}	
	
	protected void SetEditValueAndFalse( EditText editText,  int index , String value) {
		String s ;
//		s = getContentForCar(index, value);
//		editText.setText(s);
		editText.setFocusable(false); 
		editText.setEnabled(false);
	}
	
	protected int getRowId(int index) {
		int id = 0;
		id =Integer.valueOf(getContentForCar(index, Actual_Data_Table.Id));
		return id;
	}
	
	protected String getShiShiField(String vString) {
		try {
			return Submit_System_Setting_Data_Task.J_GONGCHENGMINGCHENG_GC.getJSONObject(
					Submit_System_Setting_Data_Task.mID).getString(vString);
		} catch (JSONException e) {
			return "";
		}
	}
}


//Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//strImgPath = Environment.getExternalStorageDirectory().toString() + "/CONSDCGMPIC/";//存放照片的文件夹
//String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";//照片命名
//File out = new File(strImgPath);
//if (!out.exists()) {
//        out.mkdirs();
//}
//out = new File(strImgPath, fileName);
//strImgPath = strImgPath + fileName;//该照片的绝对路径
//Uri uri = Uri.fromFile(out);
//imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//startActivityForResult(imageCaptureIntent, RESULT_CAPTURE_IMAGE);
